package com.ndp.fb.mongo.api;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.DBObject;
import com.ndp.fb.model.business.vo.admanage.param.GroupByAdSetParam;
import com.ndp.fb.model.business.vo.admanage.param.GroupByDayParam;
import com.ndp.fb.mongo.model.AdForMongo;
import com.ndp.fb.mongo.model.AdSetForMongo;
import com.ndp.fb.mongo.model.FBAdForMongo;
import com.ndp.fb.mongo.model.ImageVersion;
import com.ndp.fb.rdb.model.AdGroup;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by jacky.cheng on 2015/6/18.
 */
public interface AdGroupMongoService {
    void save(AdGroup ad);

    void saveAll(List<AdGroup> ads);

    void upsetAdForMongos(List<FBAdForMongo> adForMongos);

    AdForMongo findAdForMongo(Long ad_id);

    AdSetForMongo findAdSetForMongo(Long adSet_id);

    List<AdForMongo> findAdForMongoHashIsNotInit(int pageNum, int limit);

    List<AdForMongo> findAdForMongoHashIsNull(int skip, int limit);

    void updateAdSetForMongo(Long adSet_id, Map<String, Object> map);

    void updateAdForMongo(Long ad_id, Map<String, Object> map);

    void updateAdForMongoHashByMd5(Long ad_id, int version, int j, String hash);

    void updateAdForMongoCreatorInfo(Long ad_id, Long actor_id, String actor_name, Long application_id, String application_name);

    void addNewImageVersion(Long ad_id, ImageVersion imageVersion);

    Long getAccountAdCount(Long account_id, Long created_time);

    void saveAccountAdSnapShot(Long account_id, Date create_time, int count);

    Long getMaxCreateUtcTime(Long account_id);

    List<AdForMongo> findAdGroupAfterCreatedUtcTime(long accountId, long createdUtcTime, String[] returnFields);

    /**
     * @param groupByAdSetParam     "fbConnectionObjectId": 118316091858310,  // 应用id，即产品id（必须项）
     *                              "platform": "google_play"                   // 应用平台（可选项，如果未选择平台，该参数可以不传）
     *                              "accountIds": [
     *                                  825708010838893,
     *                                  826214027476674,
     *                                  851599978271412,
     *                                  851600424938034
     *                              ],                                          // 账号列表（必须项，至少选择一个账号）
     *                              "adName" : ["A","B"],                     // 广告名称筛选条件（可选项，必须以数组形式传递，即使只有一个，不支持以下符号 (*.?+$^[](){}|/) ，请在前端予以过滤，只要包含这些字符，提示出错 ）
     *                              "adSetName" : ["A","B"],                  // 广告组名称筛选条件（同adName）
     *                              "campaignName" : ["A","B"],                   // campaign名称筛选条件（同adName）
     *                              "adSetId" : 12345678,                     // 广告组编号筛选条件（可选项）
     *                              "adSetStatus" : "ACTIVE",                   // 广告组状态筛选条件（必须项，可选范围： ACTIVE, PAUSED, DELETED, PENDING_REVIEW, DISAPPROVED, PREAPPROVED, PENDING_BILLING_INFO, CAMPAIGN_PAUSED, ARCHIVED, ADSET_PAUSED ）
     *                              "createTimeRange": {
     *                                  "since": "2016-01-19 00:00:00",
     *                                  "until": "2016-01-21 00:00:00"
     *                              },                                          // 创建时间筛选条件（必须项，请根据当前登陆用户时区将选择的时间转化为UTC时间即零时区时间，精确到秒，格式请参考示例）
     *                              "statsTimeRange": {
     *                                  "since": "2016-01-19",
     *                                  "until": "2016-01-21"
     *                              },                                          // 统计时间筛选条件（必须项，不需要转换时区，精确到天，格式请参考示例）
     *                              "pSize": 20,                              // 分页大小（必须项，正整数）
     *                              "cPage": 1,                                   // 请求第几页（必须项，从1开始）
     *                              "sort" : {
     *                                  "clicks" : -1
     *                              },                                          // 排序参数，其中clicks代表排序字段，取值范围请参考统计数据字段一览，-1代表排序类型（1代表升序，-1代表降序）（必须项）
     *                              "returnStatsFields" : ["clicks","ctr","cr"] // 统计数据返回字段，为了避免不必要的网络消耗，对于需要返回的统计数据通过这里指定，取值范围请参考统计数据字段一览（必须项）
     * @return
     */
    Map<String, Object> groupByAdset(GroupByAdSetParam groupByAdSetParam);

    /**
     * 根据天进行Group获取数据
     *
     * @param groupByDayParam "id" : 6040592689716, // 必须项，统计对象的id，支持广告Id(adId), 广告组Id(adSetId)和广告账号Id(accountId)
     *                        "statsTimeRange": {
     *                        "since": "2016-01-19",
     *                        "until": "2016-01-21"
     *                        }, // 必须项，统计时间筛选条件（不需要转换时区，精确到天，格式请参考示例）
     * @return
     */
    List<Map<String, Object>> groupByDay(GroupByDayParam groupByDayParam);

    /**
     * 根据adSetId查询adSet，并且携带ad信息
     *
     * @param adSetId adSet的Id
     * @return 查询到的adSet
     */
    AdSetForMongo findAdSetWithAdsByAdSetId(Long adSetId);

    /**
     * 根据账号获取账号下未删除的campaign
     *
     * @param account_id
     * @param skip
     * @param limit
     * @return
     */
    List<Long> getNoDeletedCampaignId(Long account_id, int skip, int limit);

    /**
     * 根据状态和账号统计广告数量
     *
     * @return
     */
    public Map<Long, Map<String, Integer>> getAdCountGroupByAccountIdStatus();

    /**
     * 根据营销目标ID查找所有的广告
     *
     * @param adMarketTargetId 营销目标id
     * @return 查找到的广告
     * @returnFields 需要返回的字段
     */
    List<AdForMongo> findAdByAdMarketTargetId(String adMarketTargetId, String... returnFields);

    /**
     * 根据ad进行分组，专门用于营销目标详细列表的查询
     *
     * @param param 查询参数: {"ecReport":true, "adMarketTargetId":1, "adIds":[1,2,3],"returnStatsFields":["todaySpent","spent","clicks","ctr","cpc","purchase","purchaseR","cps","salesAmount","roi"],"statsTimeRange":{"since":"2016-06-10","until":"2016-06-16"},"sort":{"field":"clicks","type":-1},"today":"2016-06-16","pSize":"10","cPage":1}
     * @return 查询结果:
     * {
     * "ads" : [
     * {
     * "adId" : NumberLong(6058661841320),
     * "accountId" : NumberLong(923004234442603),
     * "adStatus" : "ACTIVE",
     * "adConfiguredStatus" : "ACTIVE",
     * "adCreatedUtcTime" : NumberLong(1461924625000),
     * "adName" : "1-B-P-160429-910",
     * "adSetId" : NumberLong(6058661840120),
     * "adSetStatus" : "ACTIVE",
     * "adSetCreatedUtcTime" : NumberLong(1461924624000),
     * "adSetConfiguredStatus" : "ACTIVE",
     * "adSetName" : "US-PC-RHS-View10-Ocpm",
     * "bidAmount" : NumberInt(20000),
     * "budget" : NumberInt(10000),
     * "campaignName" : "US-PC-All-View10-RHS-Stylewe",
     * "todaySpent" : NumberInt(0),
     * "spent" : NumberInt(47984),
     * "clicks" : NumberInt(1057),
     * "impressions" : NumberInt(181452),
     * "purchase" : NumberInt(7),
     * "cart" : NumberInt(88),
     * "salesAmount" : 76759.0,
     * "ctr" : 0.005825232017282808,
     * "cpc" : 45.39640491958373,
     * "purchaseR" : 0.07954545454545454,
     * "cps" : 6854.857142857143,
     * "roi" : 1.5996790596865622
     * }
     * ],
     * "summary" : {
     * "adCount" : NumberInt(53),
     * "todaySpent" : NumberInt(0),
     * "spent" : NumberInt(829837),
     * "clicks" : NumberInt(8410),
     * "ctr" : 0.006485009295694062,
     * "cpc" : 98.67265160523186,
     * "purchase" : NumberInt(161),
     * "purchaseR" : 1.0,
     * "cps" : 5154.267080745341,
     * "salesAmount" : 2332920.0,
     * "roi" : 2.8112990864470975
     * }
     * }
     */
    JSONObject groupByAdForAdMarketTarget(JSONObject param);


    /**
     * 根据名称查询
     *
     * @param adNames
     * @return
     */
    List<AdForMongo> findByNameIn(Set<String> adNames);

    List<DBObject> findHasMaterieIdAndNoMaterieDetail();

    /**
     * @param query  the selection criteria for the update
     * @param update modifications to apply
     * @param upsert when true, inserts a document if no document matches the update query criteria
     * @param multi  when true, updates all documents in the collection that match the update query criteria, otherwise only updates one
     */
    void update(DBObject query, DBObject update, boolean upsert, boolean multi);
}
